package com.gl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gl.entity.EduCourse;
import com.gl.entity.EduTeacher;
import com.gl.entity.TOrder;
import com.gl.entity.UcenterMember;
import com.gl.mapper.TOrderMapper;
import com.gl.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gl.service.UcenterMemberService;
import com.gl.utils.JwtUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Autowired
    private UcenterMemberService memberService;

    //分页查询订单信息
    public Map<String,Object> getPageList(long current, long size){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        Page<TOrder> page = new Page<>(current,size);
        //分页查询
        baseMapper.selectPage(page,null);
        return null;
    }

    //立即购买课程，生成订单，返回生成的订单号
    @Override
    public String saveOrder(TOrder order, HttpServletRequest request) {
        String orderNo = "";//订单号
        //获取用户信息
        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        //查询用户是否有该课程订单,如果有不添加订单
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("course_id",order.getCourseId());
        TOrder tOrder = baseMapper.selectOne(wrapper);
        if(tOrder==null){
            //UUID生成订单号
            orderNo = UUID.randomUUID().toString().replace("-", "");
            //填充最终订单添加对象
            TOrder finalOrder = new TOrder();
            finalOrder.setOrderNo(orderNo);//订单号
            finalOrder.setPayType(1);//支付类型 1：微信
            finalOrder.setStatus(0);//支付状态 0：未支付
            finalOrder.setMemberId(memberId);//用户id
            finalOrder.setNickname(member.getNickname());//用户昵称
            finalOrder.setMobile(member.getMobile());//用户手机号
            finalOrder.setCourseId(order.getCourseId());//课程id
            finalOrder.setCourseTitle(order.getCourseTitle());//课程名称
            finalOrder.setCourseCover(order.getCourseCover());//课程封面
            finalOrder.setTotalFee(order.getTotalFee());//课程价格
            finalOrder.setTeacherName(order.getTeacherName());//讲师名称
            //添加到订单表
            baseMapper.insert(finalOrder);
        }else{
            orderNo = tOrder.getOrderNo();
        }


        return orderNo;
    }
    //查订单列表
    @Override
    public Map<String,Object> getOrderList(Page<TOrder> page, TOrder order) {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        if(order!=null){//判断条件是否存在
            if(!StringUtils.isEmpty(order.getOrderNo())){
                wrapper.eq("order_no",order.getOrderNo());
            }
            if(!StringUtils.isEmpty(order.getStatus())){
                wrapper.eq("status",order.getStatus());
            }
        }
        baseMapper.selectPage(page,wrapper);
        map.put("page",page);//分页对象
        map.put("hasPrevious",page.hasPrevious());//是否存在上一页
        map.put("hasNext",page.hasNext());//是否存在下一页

        return map;

    }

}
